#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

int checkCouncilRoom(int p, struct gameState *state) {
	
	int r, buys, discardCount, handCount, deckCount, newDiscardCount, newHandCount, newDeckCount, newBuys;
	
	discardCount = state->discardCount[p];
	handCount = state->handCount[p];
	deckCount = state->deckCount[p];
	buys = state->numBuys;

	r = Council_Room(p, state, 0);

	assert(r == 0);
	printf("test passed!\n");

	newDiscardCount = state->discardCount[p];
	newHandCount = state->handCount[p];
	newDeckCount = state->deckCount[p];
	newBuys = state->numBuys;

	if (deckCount + discardCount >= 4) {
		if (deckCount >= 4) {
			assert(newDeckCount == deckCount - 4);
			printf("deck test passed!\n");
			assert(newDiscardCount == discardCount);
			printf("discard test passed!\n");
			assert(newHandCount == handCount + 3);
			printf("hand test passed!\n");
		}
		else {
			assert(newDeckCount + newDiscardCount == deckCount + discardCount - 4);
			printf("deck and discard test passed!\n");
			assert(newHandCount == handCount + 3);
			printf("hand test passed!\n");
		}
	}
	else {
		assert(newDeckCount == 0);
		printf("deck test passed!\n");
		assert(newDiscardCount == 0);
		printf("discard test passed!\n");
		assert(newHandCount == handCount + deckCount + discardCount - 1);
		printf("hand test passed!\n");
	}

	endTurn(state);

	newHandCount = state->handCount[p];
	newDiscardCount = state->discardCount[p];
	newDeckCount = state->deckCount[p];

	assert(newDeckCount + newDiscardCount + newHandCount == deckCount + discardCount + handCount);
	printf("after turn test passed!\n");
	
	return 0;
}

int main() {

        int r, p, discardCount, handCount, deckCount;
        int k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, great_hall};

        struct gameState G;

        for (p = 0; p < 2; p++) {
                for (discardCount = 0; discardCount < 10; discardCount++) {
                        for (handCount = 0; handCount < 10; handCount++) {
                                for (deckCount = 0; deckCount < 10; deckCount++) {
                                        memset(&G, 23, sizeof(struct gameState));
                                        r = initializeGame(2, k, 1, &G);
                                        G.discardCount[p] = discardCount;
                                        memset(&G, 0, sizeof(int) * discardCount);
                                        G.handCount[p] = handCount;
                                        memset(&G, 0, sizeof(int) * handCount);
                                        G.deckCount[p] = deckCount;
                                        memset(&G, 0, sizeof(int) * deckCount);
                                        checkCouncilRoom(p, &G);
                                }
                        }
                }
        }
	return 0;
}
